package io.bsly.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bsly.common.utils.Constant;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.modules.app.entity.localEntity.DyCouponEntity;
import io.bsly.modules.app.entity.localEntity.DyOfficalOrderEntity;
import io.bsly.modules.app.entity.localEntity.UserEntity;
import io.bsly.modules.app.service.localmaster.DyCouponService;
import io.bsly.modules.app.service.localmaster.DyOfficalOrderService;
import io.bsly.modules.app.service.localmaster.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;



/**
 * 记录抖音订单和兑换码  及  品名对应的关系
 *
 * @author fys
 * @email demo@gmail.com
 * @date 2021-09-27 10:00:13
 */
@Api(tags = "抖音券码")
@RestController
@RequestMapping("app/dyCoupon")
public class DyCouponController {
    @Autowired
    private DyCouponService dyCouponService;

    @Autowired
    DyOfficalOrderService officalOrderService;

    @Autowired
    UserService userService;
    /**
     * 列表
     */
    @ApiOperation(value="券列表", notes = "抖音兑换券，入参为map类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dyOrderCode",value = "抖音订单号" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "createDate",value = "申请时间Date" , dataType = "string", paramType = "query",example = "2021-09-28"),
            @ApiImplicitParam(name = "createTime",value = "申请时间Time" , dataType = "string", paramType = "query",example = "2021-09-28 14:14:14"),
            @ApiImplicitParam(name = "productName",value = "品名" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "productId",value = "卡Id" , dataType = "int", paramType = "query"),

            @ApiImplicitParam(name = "status",value = "状态值,多个用逗号分隔" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "用户ID" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页数" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "string", paramType = "query")
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> map, HttpServletRequest request){
        PageUtils pageUtils = dyCouponService.queryPage(map);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @ApiOperation(value="查询", notes = "根据ID进行查询")
    @GetMapping("/info/{id}")
    public R info(@ApiParam @PathVariable("id") Integer id){
		DyCouponEntity dyBookingInfo = dyCouponService.getById(id);

        return R.ok().put("dyBookingInfo", dyBookingInfo);
    }


    /**
     * 保存
     */
    @ApiOperation(value="保存", notes = "根据OrderCode或CouponCode进行查询")
    @PostMapping("/save")
    public R save(@RequestBody DyCouponEntity dyCouponEntity, HttpServletRequest request){
        DyCouponEntity one = dyCouponService.getOne(new QueryWrapper<DyCouponEntity>().lambda()
//                .eq(DyCouponEntity::getDyOrderCode, dyCouponEntity.getDyOrderCode())
                .eq(DyCouponEntity::getUserId, dyCouponEntity.getUserId())
                .eq(DyCouponEntity::getDyCouponCode, dyCouponEntity.getDyCouponCode())
                .ne(DyCouponEntity::getStatus, Constant.CouponStatus.FAIL)//对该状态下不可重复，其他状态均可
        ) ;
        if (one!=null){
            return R.error("申请信息已存在");
        }
        UserEntity userEntity = userService.getById(dyCouponEntity.getUserId());
        dyCouponEntity.setUsername(userEntity.getUsername());
        dyCouponService.save(dyCouponEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value="更新", notes = "根据ID进行更新")
    @PostMapping("/update")
    public R update(@RequestBody DyCouponEntity dyCouponEntity){
        dyCouponService.updateById(dyCouponEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value="删除", notes = "根据ID数组进行批量删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        dyCouponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 自动核销
     * 订单状态1 待确认/待支付（订单创建完毕）105 已支付 2 备货中 101 部分发货 3 已发货（全部发货）4 已取消5 已完成（已收货）
     */
    @ApiOperation(value="自动核销", notes = "根据劵码进行操作")
    @GetMapping("/autoVerify/{certNo}")
    public R autoVerifyV2(@PathVariable("certNo") String certNo){
        DyOfficalOrderEntity orderEntity = officalOrderService.getByCouponNo(certNo);
        //判断订单状态，券码状态

        //券码未核销 进行核销

        //保存用户券码信息到表

        return R.ok();
    }


}
